package com.ocom.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.company.EntityDockUrl;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.company.dao.IDaoDockUrl;
import com.ocom.company.service.DockUrlService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.apache.commons.lang3.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.stereotype.Service;

@Service
public class DockUrlServiceImpl extends ServiceImpl<IDaoDockUrl, EntityDockUrl> implements DockUrlService {

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB)
	public EntityDockUrl getDockUrl(String dockCode, Integer grade, Long comId, String serverCode) {
		QueryWrapper<EntityDockUrl> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dock_code", dockCode);
		if (grade == 0) {
			queryWrapper.eq("com_id", comId);
		}
		if (grade == 10) {
			queryWrapper.eq("server_code", serverCode);
		}
		return this.getOne(queryWrapper);
	}

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public EntityDockUrl getDockUrl(String dockCode, Integer grade, Long comId, String serverCode, String dockApiCode) {
        QueryWrapper<EntityDockUrl> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dock_code", dockCode);
        queryWrapper.eq("dock_api_code", dockApiCode);
        queryWrapper.eq("grade", grade);
        if (grade == 0) {
            queryWrapper.eq("com_id", comId);
        }
        if (grade == 10) {
            queryWrapper.eq("server_code", serverCode);
        }
        return this.getOne(queryWrapper);
    }


    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public void setDockApiUrl(EntityDockUrl request) {
        QueryWrapper<EntityDockUrl> queryWrapper = Wrappers.query();
        LambdaQueryWrapper<EntityDockUrl> lambda = queryWrapper.lambda();
        lambda.eq(EntityDockUrl::getDockCode, request.getDockCode())
                .eq(EntityDockUrl::getDockApiCode, request.getDockApiCode())
                .eq(EntityDockUrl::getGrade, request.getGrade());
        if (10 == request.getGrade())lambda.eq(EntityDockUrl::getServerCode, request.getServerCode());
        if (0 == request.getGrade())lambda.eq(EntityDockUrl::getComId, request.getComId());
        lambda.last("limit 1");
        EntityDockUrl dockUrl = this.getOne(queryWrapper);
        if (null == dockUrl) {
            if(StringUtils.isNotBlank(request.getRequestUrl())) {
                this.save(request);
            }
        } else {
            if (StringUtils.isBlank(request.getRequestUrl())) {
                this.removeById(dockUrl.getId());
            } else {
                dockUrl.setRequestUrl(request.getRequestUrl());
                this.updateById(dockUrl);
            }
        }
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public boolean removeDockUrlByComIdAndDockCodeAndGrade(Long comId, String dockCode, Integer grade) {
        QueryWrapper<EntityDockUrl> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("dock_code", dockCode);
        queryWrapper.eq("grade", grade);
        return this.remove(queryWrapper);
    }
}
